package com.example.nacosconfig.controller;

import com.example.nacosconfig.redis.RedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private String useLocalCache;

    @Autowired
    public RedisTemplate redisTemplate;

    // 设置参数
    // curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=xxxxxxxxxx"

    // 读取参数
    // http://localhost:8080/config/get
    @RequestMapping("/get")
    public String get() {
        return useLocalCache;
    }

    @RequestMapping("/getName")
    public Object getName(){
        RedisRepository re = new RedisRepository(redisTemplate);
        re.set("name","malcolm");
        return re.get("name");
    }

    @Value("${common1.configuration1}")
    private String common1Configuration;

    @Value("${common2.configuration2}")
    private String common2Configuration;

    @Value("${common3.configuration3}")
    private String common3Configuration;

    @Value("${common4.configuration4}")
    private String common4Configuration;

    @Value("${common5.configuration5}")
    private String common5Configuration;

    @Value("${common6.configuration6}")
    private String common6Configuration;

//    @Value("${common7.configuration7}")
//    private String common7Configuration;
//
//    @Value("${common8.configuration8}")
//    private String common8Configuration;


    @GetMapping("/test_common_configuration1")
    public String testCommonConfiguration1() {
        return common1Configuration;
    }

    @GetMapping("/test_common_configuration2")
    public String testCommonConfiguration2() {
        return common2Configuration;
    }

    @GetMapping("/test_common_configuration3")
    public String testCommonConfiguration3() {
        return common3Configuration;
    }

    @GetMapping("/test_common_configuration4")
    public String testCommonConfiguration4() {
        return common4Configuration;
    }

    @GetMapping("/test_common_configuration5")
    public String testCommonConfiguration5() {
        return common5Configuration;
    }

    @GetMapping("/test_common_configuration6")
    public String testCommonConfiguration6() {
        return common6Configuration;
    }

//    @GetMapping("/test_common_configuration7")
//    public String testCommonConfiguration7() {
//        return common7Configuration;
//    }
//
//    @GetMapping("/test_common_configuration8")
//    public String testCommonConfiguration8() {
//        return common8Configuration;
//    }
}